package org.qs.webservice.service.impl;

import java.util.Date;

import org.qs.webservice.content.IReadContent;
import org.qs.webservice.service.IreadMemcacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.danga.MemCached.MemCachedClient;

@Service
public class IreadMemcacheServiceImpl implements IreadMemcacheService{
	
	
	@Autowired
	private MemCachedClient memCachedClient;  
	
	public Object get(String key) {  
		Object o = memCachedClient.get(key);
		if(o==null){
			key = IReadContent.pre_mencache_key+key;
			o = memCachedClient.get(key);
		}
        return o ;  
    }  
      
    public boolean set(String key,int time, Object value) {  
    	key = IReadContent.pre_mencache_key+key;
        return memCachedClient.set(key, value,new Date(time*1000));  
    }
    public boolean replace(String key,int time, Object value) {  
    	key = IReadContent.pre_mencache_key+key;
        return memCachedClient.replace(key, value,new Date(time*1000));  
    }  
      
    public boolean delete(String key) {  
        return memCachedClient.delete(key);  
    }

    public boolean setUserSignIn(String key,Object value){
		boolean ret = false;
		key="usersignIn_api_"+key;
		if(get(key)==null){
			ret=set(key,IReadContent.user_sign_in_effective_time, value);
		}else{
			ret=replace(key,IReadContent.user_sign_in_effective_time, value);
		}
		return ret;
	}
	public Object getUserSignIn(String key){
		key="usersignIn_api_"+key;
		return get(key);
	}

	@Override
	public boolean setUserRestPsswd(String key, Object value) {
		boolean ret = false;
		key="reset_passwd_api_"+key;
		if(get(key)==null){
			ret=set(key,IReadContent.mobile_reset_passwd_effective_time, value);
		}else{
			ret=replace(key,IReadContent.mobile_reset_passwd_effective_time, value);
		}
		return ret;
	}

	@Override
	public Object getUserRestPsswd(String key) {
		key="reset_passwd_api_"+key;
		return get(key);
	}

	@Override
	public boolean deleteUserRestPsswd(String key) {
		key="reset_passwd_api_"+key;
		return delete(key);
	}

	@Override
	public Object getBookReview(String key) {
		key="book_review_"+key;
		return get(key);
	}

	@Override
	public boolean setetBookReview(String key,String value) {
		boolean ret = false;
		key="book_review_"+key;
		if(get(key)==null){
			ret=set(key,IReadContent.book_rewiew_effective_time, value);
		}else{
			ret=replace(key,IReadContent.book_rewiew_effective_time, value);
		}
		return ret;
	}

	@Override
	public Object getBookReviewReply(String key) {
		key="book_review_reply_"+key;
		return get(key);
	}

	@Override
	public boolean setetBookReviewReply(String key, String value) {
		boolean ret = false;
		key="book_review_reply_"+key;
		if(get(key)==null){
			ret=set(key,IReadContent.book_rewiew_reply_effective_time, value);
		}else{
			ret=replace(key,IReadContent.book_rewiew_reply_effective_time, value);
		}
		return ret;
	}
	
   

	

   
}
